<?php
namespace backend\controllers;


use backend\component\BackendBaseController;
use backend\component\ResultFormat;
use common\models\info\Album;
use backend\service\AlbumService;
use backend\service\PhotoService;
use yii\helpers\ArrayHelper;

class AlbumController extends BackendBaseController
{
    public function actions()
    {
        return [
            'error' => [
                'class' => 'frontend\actions\DefaultErrorAction'
            ],
            "modify" => [
                'class' => "backend\actions\ModifyAction",
                'modelName'=> Album::className(),
            ],
            "delete" =>[
                'class' => "backend\actions\DeleteAction",
                'modelName'=> Album::className(),
                'is_physics' => true
            ],
        ];
    }
    public function actionAdd(){
        $params = \Yii::$app->request->post("form");
        $model = Album::modelById(0);
        $model->setAttributes($params);
        if (!$model->save()) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("操作成功");
    }
    public function actionList(){
        $get = \Yii::$app->request->get();
        $data = AlbumService::AlbumList($get);
        return ResultFormat::success('SUCCESS',json_decode($data));
    }
    public function actionEdit(){
        $id = \Yii::$app->request->get("id",0);
        $model = Album::modelById($id);
        return ResultFormat::success('SUCCESS',$model->attributes);
    }
    public function actionModify(){
        $id = \Yii::$app->request->get("id",0);
        $params = \Yii::$app->request->post("form");
        $model = Album::modelById($id);
        $model->setAttributes($params);
        $model->created_at =time();
        if (!$model->save()) {
            return ResultFormat::error($model->getErrorsToString());
        }
        return ResultFormat::success("操作成功");
    }

    public function actionPhotos(){
        $GET = \Yii::$app->request->get();
        $data = PhotoService::PhotoList($GET,$page=1,$pageSize=10);
        return ResultFormat::success('SUCCESS',json_decode($data));
    }
}